# Genesis, the ColdC driver

INCLUDE=-Iinclude -Imodules
CFLAGS=@CFLAGS@ $(INCLUDE)
MCFLAGS=`echo $(CFLAGS) | sed -e 's:-pedantic::'`
LIBS=@LIBS@
LDFLAGS=@LDFLAGS@ -Lmodules
CC=@CC@
RANLIB=@RANLIB@
LINT=@LINT@
YACC=@YACC@
YFLAGS=-d

SYS=@SYS@
CPU=@CPU@
VENDOR=@VENDOR@

prefix=@prefix@
MODE=@MODE@
USER=@USER@
BINDIR=$(prefix)/bin
SRCDIR=$(prefix)/src
MODULES=$(SRCDIR)/modules
OPSDIR=$(SRCDIR)/ops
DATADIR=$(SRCDIR)/data
TESTDIR=$(prefix)/test

VERSION=@VERSION@
VMAJOR=@VMAJOR@
VMINOR=@VMINOR@
VPATCH=@VPATCH@
VRELEASE=@VRELEASE@
INSTALL=@INSTALL@

DATA_O     = data/data.o data/buffer.o data/dict.o data/ident.o\
             data/list.o data/object.o data/string.o \
	     data/handled_frob.o data/quickhash.o
GRAMMAR_O  = grammar.o
DB_O       = cache.o binarydb.o dbpack.o decode.o lookup.o
IO_O       = io.o log.o net.o file.o
MISC_O     = strutil.o memory.o sig.o util.o regexp.o defs.o dns.o
OPS_O      = ops/operators.o ops/buffer.o ops/error.o ops/list.o\
             ops/object.o ops/dict.o ops/string.o ops/data.o ops/sys.o\
             ops/misc.o ops/task.o ops/file.o ops/network.o ops/math.o
CRYPT_O    = shs.o crypt.o
PCODE_O    = codegen.o execute.o opcodes.o token.o native.o
MOD_O      = @MOD_O@
OMOD_O     = `echo $(MOD_O) | sed -e 's/modules\///g'`
COMMON_O   = $(DATA_O) $(DB_O) $(IO_O) $(MISC_O) \
             $(PCODE_O) $(OPS_O) $(MOD_O) $(CRYPT_O)
DRIVER_O   = $(GRAMMAR_O) genesis.o $(COMMON_O)
COMPILER_O = $(GRAMMAR_O) coldcc.o textdb.o $(COMMON_O)
CONTROL_O  = control.o

EVERYTHING_O = genesis.o control.o coldcc.o $(COMMON_O)
EVERYTHING_C = `echo $(EVERYTHING_O) | sed -e 's/\.o/.c/g'`

DRIVER = genesis
COMPILER = coldcc
CONTROL = control

EXES = $(COMPILER) $(DRIVER)

all: $(GRAMMAR_O) include/parse.h Data Modules Ops $(EXES)

Data:
	@echo "data objects..."
	@cd $(DATADIR); make

Modules:
	@echo "module objects..."
	@cd $(MODULES); make $(OMOD_O)

Ops:
	@echo "operator and function objects..."
	@cd $(OPSDIR); make

$(DRIVER): $(DRIVER_O)
	$(CC) $(CFLAGS) $(LDFLAGS) $(DRIVER_O) $(LIBS) -o $@

$(COMPILER): $(COMPILER_O)
	$(CC) $(CFLAGS) $(LDFLAGS) $(COMPILER_O) $(LIBS) -o $@

clean:
	rm -f include/parse.h grammar.c *~ *.BAK *.bak y.output *.core
	rm -f $(EXES)
	cd $(SRCDIR); rm -f $(EXES)
	@cd $(MODULES); make $@
	@cd $(OPSDIR); make $@
	@cd $(DATADIR); make $@
	@cd $(SRCDIR)/..; rm -rf configure autom4te.cache config.log config.status configure.lineno  

scrub: clean
	rm -f *.o include/parse.h grammar.c *~ *.BAK *.bak y.output *.core
	rm -f textdump*
	rm -f $(EXES)
	cd $(SRCDIR); rm -f $(EXES)
	rm -rf binary binary.bak
	@cd $(MODULES); make $@
	@cd $(OPSDIR); make $@
	@cd $(DATADIR); make $@
	@cd $(SRCDIR); rm -f Makefile

commitable:
	@cd $(MODULES); ./modbuild -m0 cdc web ext_math

commit: commitable
	cvs commit

makefiles: makefile

makefile:
	@cd ..; etc/config.status

test: all
	@cd $(TESTDIR);\
		ln -sf $(SRCDIR)/coldcc;\
		./runtest $(TESTDIR)

install: all
	@$(SRCDIR)/INSTALL.sh $(USER) $(MODE) $(INSTALL) $(prefix)

depend: $(GRAMMAR_O) include/parse.h
	@echo modules..
	@cd $(MODULES); make $@
	@echo ops..
	@cd $(OPSDIR); make $@
	@echo data..
	@cd $(DATADIR); make $@
	@makedepend -- $(CFLAGS) -- $(EVERYTHING_C) > /dev/null 2>&1
	@rm -f Makefile.bak

lint:
	$(LINT) $(INCLUDE) -u $(EVERYTHING_C)

# I duplicate scrub because of the recusion involved in calling modules
patchable: commitable
	rm -f *.o include/parse.h grammar.c *~ *.BAK *.bak y.output *.core
	rm -rf binary
	rm -f textdump*
	rm -f $(EXES)
	cd $(SRCDIR); rm -f $(EXES)
	@cd $(MODULES); make $@
	@cd $(OPSDIR); make $@
	@cd $(DATADIR); make $@
	rm -f $(prefix)/etc/config.cache
	rm -f $(prefix)/etc/config.log
	rm -f $(prefix)/etc/config.status
	rm -f $(MODULES)/modbuild.last
	rm -f $(SRCDIR)/Makefile
	rm -f $(SRCDIR)/modules/Makefile
	rm -f $(SRCDIR)/include/config.h
	rm -f $(TESTDIR)/coldcc

include/parse.h:
	mv -f y.tab.h include/parse.h

reconfig: scrub config
config:
	@cd ../; configure

win32: Win32

Win32:
	@echo "cleaning heirarchy..."
	@make scrub 2>&1 > /dev/null
	@echo "creating config file..."
	@sed -e "s/#VMAJOR#/#define VERSION_MAJOR $(VMAJOR)/;\
                s/#VMINOR#/#define VERSION_MINOR $(VMINOR)/;\
                s/#VPATCH#/#define VERSION_PATCH $(VPATCH)/" include/config.h.win32 > include/config.h
	@echo "generating parser source..."
	@$(YACC) $(YFLAGS) grammar.y
	@mv -f y.tab.c grammar.c
	@mv -f y.tab.h include/parse.h
	@grep '#include' grammar.c > incs
	@grep -v '#include' grammar.c > out
	@cat incs out > grammar.c
	@rm incs out
